/**
 * Avocado.util.AvocadoScrollBar
 * 
 * classe destinada a criação de uma Scroollbar para um movieclip.
 * 
 * obs: a altura da barra de arrasto deve ser atualizada cada vez em que se
 * atualiza a altura do target.
 * 
 * @TODO
 * - suporte horizontal;
 * 
 * ***/
package Avocado.util {

	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class AvocadoScrollBar extends MovieClip {
		
		private var target:DisplayObject;
		private var targetOX:Number;
		private var targetOY:Number;
		private var theMask:Rectangle;
		
		public var theMaskClip:Sprite;
		
		public var bgBar:MovieClip;
		public var dragBar:MovieClip;
		public var setaUp:MovieClip;
		public var setaDown:MovieClip;
		
		private var dragArea:Rectangle;
		private var dragIsDrag:Boolean = new Boolean(false);

		public function AvocadoScrollBar(_target:DisplayObject, _theMask:Rectangle, _bgBar:MovieClip=null, _dragBar:MovieClip=null, _setaUp:MovieClip=null, _setaDown:MovieClip=null) {
			super();
			
			this.target = _target;
			this.targetOX = target.x;
			this.targetOY = target.y;

			this.theMask = _theMask;
			
			this.bgBar = _bgBar;
			this.dragBar = _dragBar;
			this.setaUp = _setaUp;
			this.setaDown = _setaDown;
			
			this.addEventListener(Event.ADDED_TO_STAGE, initThis);
		}
		
		/**
		 * 
		 * ***/
		private function initThis(e:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE, initThis);
			setMaskOnTarget();
			setupBars();
		}


		/**
		 * 
		 * ***/
		private function setMaskOnTarget():void{
			theMaskClip = new Sprite;
			theMaskClip.graphics.beginFill(0xff1111, .44);
			theMaskClip.graphics.drawRect(theMask.x, theMask.y, theMask.width, theMask.height);
			theMaskClip.graphics.endFill();
			Object(this.parent).addChild(theMaskClip);
			
			target.mask = theMaskClip;
			target.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
		}
		
		private function setupBars():void{
			dragArea = new Rectangle(0, 0, 0, theMask.height - dragBar.height);
			
			bgBar.height = theMask.height;
			addChild(bgBar);

			dragBar.y = 0;
			dragBar.buttonMode = true;
			dragBar.addEventListener(MouseEvent.MOUSE_DOWN, downDrag);
			dragBar.addEventListener(MouseEvent.MOUSE_UP, upDrag);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, moveDrag);
			addChild(dragBar);

			setaUp.y = 0;
			setaUp.buttonMode = true;
			setaUp.addEventListener(MouseEvent.CLICK, clickUp);
			addChild(setaUp);
			
			setaDown.y = bgBar.height;
			setaDown.buttonMode = true;
			setaDown.addEventListener(MouseEvent.CLICK, clickDown);
			addChild(setaDown);

			upDragBar()
		}
			private function updateBars():void{
				dragArea = new Rectangle(0, 0, 0, theMaskClip.height - dragBar.height);
				bgBar.height = theMaskClip.height;
				setaDown.y = theMaskClip.height;
			}
		
		private function downDrag(e:MouseEvent):void{
			dragBar.startDrag(false, dragArea);
			dragIsDrag = true;
			this.stage.addEventListener(MouseEvent.MOUSE_UP, upDrag);
		}

		private function upDrag(e:MouseEvent):void{
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, upDrag);
			dragIsDrag = false;
			dragBar.stopDrag();
		}
		
		private function moveDrag(e:MouseEvent=null):void{
			if (dragIsDrag == true){
				target.y = -( ( (target.height - theMaskClip.height) / (theMaskClip.height - dragBar.height) ) * dragBar.y ) + targetOY;
			}
		}
		
		private function updateByClick():void{
			TweenMax.to(target, .3, {y: -( ( (target.height - theMaskClip.height) / (theMaskClip.height - dragBar.height) ) * dragBar.y ) + targetOY });
		}
		
		

		/**
		 * ajusta a altura para baixo
		 * ***/
		private function clickDown(e:MouseEvent=null):void{
			dragBar.y += (theMaskClip.height - dragBar.height) * 0.1;
			if ( dragBar.y > (theMaskClip.height - dragBar.height) ) {
				dragBar.y = theMaskClip.height - dragBar.height;
			}
			updateByClick();
		}


		/**
		 * ajusta a altura para cima
		 * ***/
		private function clickUp(e:MouseEvent=null):void{
			dragBar.y -= (theMaskClip.height - dragBar.height) * 0.1;
			if ( dragBar.y < 0 ) {
				dragBar.y = 0;
			}
			updateByClick();
		}
		
		/**
		 * modifica a altura de acordo com o rolamento do mouse
		 * ***/
		private function mouseWheel(e:MouseEvent):void{
			if (e.delta > 0) {
				clickUp();
			} else {
				clickDown();
			}
		}
		

		

		/**
		 * atualiza a altura da barra de arrasto.
		 * ***/
		public function upDragBar(dragHeight:Number=0):void{
			if (dragHeight <= 0) {
				var dbH:Number = new Number(theMaskClip.height - (target.height - theMaskClip.height));
				dragBar.height = dbH *.2;
				dragArea = new Rectangle(0, 0, 0, theMask.height - dragBar.height);
	
				if (target.height < theMask.height) {
					disableThis();
				} else {
					enableThis();
					updateBars();
				}
			} else {
				dragBar.height = dragHeight;
				dragArea = new Rectangle(0, 0, 0, theMask.height - dragBar.height);

				enableThis();

				updateBars();
			}
		}

		
		/**
		 * 
		 * ***/
		public function disableThis():void{
			target.removeEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			bgBar.visible = false;
			dragBar.visible = false;
			setaUp.visible = false;
			setaDown.visible = false;
		}


		/**
		 * 
		 * ***/
		public function enableThis():void{
			if (!target.hasEventListener(MouseEvent.MOUSE_WHEEL)){
				target.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			}
			bgBar.visible = true;
			dragBar.visible = true;
			setaUp.visible = true;
			setaDown.visible = true;
		}
		
		

		/**
		 * desabilita o scroll via mouse 
		 * ***/
		public function disableMouse():void{
			target.removeEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
		}

		/**
		 * habilita o scroll via mouse
		 * ***/
		public function enableMouse():void{
			if (!target.hasEventListener(MouseEvent.MOUSE_WHEEL)){
				target.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			}
		}

		
				
	}
}